﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using EmptyWallGallery.Core;
using EmptyWallGallery.Core.BLL;
using EmptyWallGallery.Core.Utilities;

namespace EmptyWallGallery.FrontEnd.inside.Editor.Video
{
    public partial class Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            BindGrid();

            Page.Title = "Quản lý video - " + Configs.WebTitle;
        }

        public void BindGrid()
        {
            switch (Request.QueryString["action"])
            {
                case "Delete":
                    //Delete
                    if (Request.QueryString["ID"] != null)
                    {
                        if (Common.Instance.IsNumeric(Request.QueryString["ID"]))
                        {
                            Videos.Instance.Delete(Convert.ToInt32(Request.QueryString["ID"]));
                            //DeleteArticle(int.Parse(Request.QueryString["ID"]));

                            Response.Redirect(Request.QueryString["ReUrl"] ?? "default.aspx", false);
                        }
                    }
                    break;
                default:
                    grdListVideo.DataSource = Videos.Instance.GetAll(true, false);
                    grdListVideo.DataBind();
                    break;
            }
        }

        protected void BtnApplyClick(object sender, EventArgs e)
        {
            const int i = 0;
            if (Request.Form["selectedAction0"] == "1" | Request.Form["selectedAction1"] == "1")
            {
                //Edit Category
                var vId = "";
                foreach (var str in Regex.Split(Request.Form["cboCheckArticle"], ",").Where(str => i == 0))
                {
                    vId = str;
                    if (!Common.Instance.IsNumeric(vId))
                    {
                        vId = "1";
                    }
                    break; // TODO: might not be correct. Was : Exit For
                }
                Response.Redirect("VideoUpdate.aspx?action=Edit&ID=" + vId + "&ReUrl=" + Request.RawUrl, false);
            }
            else if (Request.Form["selectedAction0"] == "2" || Request.Form["selectedAction1"] == "2")
            {
                //Delete
                foreach (var str in Regex.Split(Request.Form["cboCheckArticle"], ","))
                {
                    Videos.Instance.Delete(Convert.ToInt32(str));
                }
                BindGrid();
            }
        }

        protected void BtnApply2Click(object sender, EventArgs e)
        {
            BtnApplyClick(sender, e);
        }

        protected void GrdListVideoPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdListVideo.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        public string DisplayTitle(string title)
        {
            string strOut;
            if (title.Length > 62)
            {
                strOut = title.Substring(0, 62) + "...";
            }
            else
            {
                strOut = title;
            }
            return strOut;
        }

        protected string DisplayStatus(object videoStatus)
        {
            return Convert.ToBoolean(videoStatus) ? "<br />Chưa đăng" : "<br />Đã đăng";
        }

        public object DisplayStatusDate(string d)
        {
            return string.Format("{0:dd/MM/yyyy}", DateTime.Parse(d).AddHours(Configs.TimeLine));
        }

        protected string WriteVideoImage(object videoImage, object objCreateDate, object objUserId)
        {
            var imgUrl = "";
            var createDate = Convert.ToDateTime(objCreateDate);
            var membershipUser = Membership.GetUser(objUserId);
            if (membershipUser != null)
            {
                imgUrl = string.Format("/Uploads/{0}/{1}/{2}/{3}/{4}{5}", membershipUser.UserName, createDate.Year,
                                           Common.Instance.Right("0" + createDate.Month, 2),
                                           Common.Instance.Right("0" + createDate.Day, 2), "100x0__", videoImage);
            }
            return "<img src='" + imgUrl + "' width='100' />";
        }
    }
}